package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.vo.CheckBox;
import com.cy.pj.common.vo.PageObject;
import com.cy.pj.common.vo.SysRoleMenuVo;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.pojo.SysRole;
import com.cy.pj.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author YuBaoFu
 * @create 2025/06/03
 * @description
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;


    @Override
    public PageObject<SysRole> findPageObjects(String name, Integer pageCurrent) {

        if (pageCurrent == null || pageCurrent < 1){
            throw new ServiceException("当前页码值无效，请重新输入");
        }
        int rowCount = sysRoleDao.getCount(name);
        if (rowCount < 1){
            throw new ServiceException("没有查询到对应的数据，请稍后重试");
        }
        /* 计算分页条数 */
        Integer pageSize = 2;
        Integer startIndex = (pageCurrent-1) * pageSize;
        List<SysRole> list = sysRoleDao.findPageObjects(name, startIndex, pageSize);
        return new PageObject<>(pageCurrent,pageSize,rowCount,list);
    }

    @Override
    @Transactional
    public int deleteObject(Integer id) {
        if (id==null || id<=0){
            throw new ServiceException("请输入合法的ID");
        }

        sysRoleMenuDao.deleteObjectsByRoleId(id);
        sysUserRoleDao.deleteObjectsByRoleId(id);
        int rows = sysRoleDao.deleteObject(id);
        if (rows == 0){
            throw new ServiceException("删除失败，请稍后重试");
        }
        return rows;
    }

    @Override
    public int saveObject(SysRole sysRole, Integer[] menuIds) {

        if (sysRole == null){
            throw new ServiceException("保存对象不能为空");
        }
        if (StringUtils.isEmpty(sysRole.getName())){
            throw new ServiceException("保存的角色名不能为空");
        }
        if (menuIds==null || menuIds.length<1){
            throw new ServiceException("必须给角色分配权限!");
        }
        sysRole.setCreatedUser("wpl");
        sysRole.setModifiedUser("wpl");
        int rows = sysRoleDao.insertObject(sysRole);
        System.err.println(rows);
        if (rows < 1){
            throw new ServiceException("角色保存失败，请稍后重试");
        }
        sysRoleMenuDao.insertObjects(sysRole.getId(),menuIds);
        return rows;
    }

    @Override
    public SysRoleMenuVo findObjectById(Integer id) {

        if (id==null || id<=1){
            throw new ServiceException("请输入合法的ID");
        }
        SysRoleMenuVo sysRoleMenuVo = sysRoleDao.findObjectById(id);
        if (sysRoleMenuVo == null){
            throw new ServiceException("没有查询到对应的数据！");
        }
        return sysRoleMenuVo;
    }

    @Override
    public int updateObject(SysRole sysRole, Integer[] menuIds) {

        if (sysRole == null){
            throw new ServiceException("保存角色不可为空");
        }
        if (sysRole.getId() ==null || sysRole.getId() <= 0){
            throw new ServiceException("保存角色ID不可为空");
        }
        if (StringUtils.isEmpty(sysRole.getName())){
            throw new ServiceException("保存角色名字不可为空");
        }
        if (menuIds==null || menuIds.length < 1){
            throw new ServiceException("保存角色的权限不可为空");
        }
        sysRole.setModifiedUser("王鹏力");
        int rows = sysRoleDao.updateObject(sysRole);
        if (rows == 0) throw new ServiceException("修改失败");
        sysRoleMenuDao.deleteObjectsByRoleId(sysRole.getId());
        sysRoleMenuDao.insertObjects(sysRole.getId(),menuIds);
        return rows;
    }

    @Override
    public List<CheckBox> findObjects() {
        return sysRoleDao.findObjects();
    }
}
